package ug.co.icom.kyazze.healthticket.mongoHQ;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.util.Log;


@SuppressWarnings("deprecation")
public class UploadToServer extends AsyncTask<Object, Void, String> {
	private final String TAG = "UploadToServer";

		protected String doInBackground(Object... arg0) {
			Log.d(TAG, "Upload To Server");
			long user_value = (Long) arg0[0];
			File directory = (File) arg0[1];
			try {
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				//bm.compress(CompressFormat.JPEG, 75, bos);
				@SuppressWarnings("unused")
				byte[] data = bos.toByteArray();
				HttpClient httpClient = new DefaultHttpClient();
				HttpPost postRequest = new HttpPost(
						"http://appitconsult.com/healthticket/upload.php");
				//ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
				 File file= new File(directory+"/"+user_value+"pub_key");
				 File file2= new File(directory+"/"+user_value+"master_key");
				 File file3= new File(directory+"/"+user_value+".cpabe");
				 File file4= new File(directory+"/"+user_value+"prv_key");		 
				 
				 FileBody bin = new FileBody(file);
				 FileBody bin2 = new FileBody(file2);
				 FileBody bin3 = new FileBody(file3);
				 FileBody bin4 = new FileBody(file4);
				MultipartEntity reqEntity = new MultipartEntity(
						HttpMultipartMode.BROWSER_COMPATIBLE);
				reqEntity.addPart("uploaded",bin);
				reqEntity.addPart("uploaded2",bin2);
				reqEntity.addPart("uploaded3",bin3);
				reqEntity.addPart("uploaded4",bin4);
				//reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
				postRequest.setEntity(reqEntity);
				HttpResponse response = httpClient.execute(postRequest);
				BufferedReader reader = new BufferedReader(new InputStreamReader(
						response.getEntity().getContent(), "UTF-8"));
				String sResponse;
				StringBuilder s = new StringBuilder();

				while ((sResponse = reader.readLine()) != null) {
					s = s.append(sResponse);
				}
				System.out.println("Response: " + s);
			} catch (Exception e) {
				// handle exception here
				Log.e(e.getClass().getName(), e.getMessage());
			}
			return null;
		}
	
	}
		

